﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AxRealLayoutPanel;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraTab;
using MINERP.BLL.Common;
using MINERP.COM;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.NewPinBan;
using MINERP.NewPB.com;
using MINERP.NewPB.IView;
using MINERP.NewPB.Presenter;
using MINERP.NewPB.Properties;
using RealLayoutPanel;
using Panel = System.Windows.Forms.Panel;

namespace MINERP.NewPB.Forms
{
    using System.Threading;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Design;


    using MessageBox = System.Windows.Forms.MessageBox;
    using System.IO;

    public partial class FrmMain2 : FrmParent, IMainView
    {
        //		private readonly List<PicView> selectedCtrls = new List<PicView>(); //被 框选 的那些图片集合

        #region PhotoBarCodeStatus enum

        public enum PhotoBarCodeStatus
        {
            RightOrLeft = 0, //条码在左或右
            TopOrBottom = 1, //条码在上或下
            NoBarCode //无条码
        }

        #endregion

        private bool HaveAPanel; //是否有多选图片的临时PANEL
        private Panel KXP; //如果有则指向该PANEL
        private bool MouseIsDown; //用于多选判断鼠标是否被按下
        private Rectangle MouseRect = Rectangle.Empty; //将要画矩形框的大小
        private bool _displayAuto;
        private MainPresenter _presenter;
        private int perPixel;
        //private GridControlAdapter gridControlAdapter;
        public FrmMain2()
        {
            _displayAuto = false;
            InitializeComponent();
            base.InitiAuthority();
            RushBoardStatus();
            //在没有选择模板前要初始化一个模块，主要用来一画刻度线
            DataTransfer.curTemplate = new Tb_paperTemplate_Info { BoardHeight = 30, BoardLength = 40 };
            //axPanel1.
            //gridControlAdapter = new GridControlAdapter(GcPhotoInfo);
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //gridControlAdapter.EnableMouseDownMoveSelectRow(gridView1);
            //cbBarCodeWidth.Text = DataTransfer.BarCodePixWidth.ToString();
            cbBarCodeWidth.SelectedIndexChanged -= cbBarCodeWidth_SelectedIndexChanged;
            cbBarCodeWidth.SelectedIndex =
                Convert.ToInt32(string.IsNullOrEmpty(IniFile.Ini.IniReadValue("PB", "BarCodeWidth"))
                                    ? "0"
                                    : IniFile.Ini.IniReadValue("PB", "BarCodeWidth"));
            cbBarCodeWidth.SelectedIndexChanged += cbBarCodeWidth_SelectedIndexChanged;
            perPixel = DataTransfer.perPix;
            pRedLine.BackColor = Color.Red;
            pRedLine.Width = 1;
            comboBox1.SelectedIndex = 0; //黙认为上方
            comboBox2.SelectedIndex = 0; //正常模式
            comboBox3.SelectedIndex = 3; //自动
            comboBoxCutType.SelectedIndex = Properties.Settings.Default.BoardType;
            //从配置文件中获取需要获取的定单数量
            cmboxOrderNum.Items.Clear();
            cmboxOrderNum.Items.Add(ConfigurationManager.AppSettings["PBorderNum"]);
            cmboxOrderNum.Items.Add("全部");
            cmboxOrderNum.SelectedIndex = 0;
            DataTransfer.PhotoDirection = 0;
            try
            {
                DataTransfer.ShowOrderNum = Convert.ToInt32(ConfigurationManager.AppSettings["PBorderNum"]);
            }
            catch (Exception)
            {
                DataTransfer.ShowOrderNum = 30;
            }
            DataTransfer.curUid = base.userId;
            //DataTransfer.curUid = 32;
            DataTransfer.curUser = base.userName;
            _presenter = new MainPresenter(this);
            comboBoxSizeUnit.SelectedIndex = 0;
            checkBoxPinFlag.Checked = Settings.Default.AutoPinban;
            //WM_MaterialItemDDO[] wmMaterialItemDdos = printerDDO.GetPaperMaterialItems(DataTransfer.CurPrinter.PrinterID);
            ////					 WM_MaterialItemDDO.GetPaperMaterialItem();
            //cbPaperMat.FillCollections(wmMaterialItemDdos, c => c.Name + c.MID, c => c.MID, false, c => c.Name);
        }
        private void cmbTemplate_DropDownClosed(object sender, EventArgs e)
        { //选择模板后Cursor = Cursors.WaitCursor;
            if (!string.IsNullOrEmpty(cmbTemplate.Text))
            {
                DataTransfer.PreSelectTemplateName = SelectTemplateName;
                _presenter.SelectedTemplate();
                //PnlBigBoard.Select();
                toolStripButton3.Enabled = true;
                toolStripButton4.Enabled = true;

                //foreach(CheckedListBoxItem item in cbPaperMat.Properties.Items) {
                //    DisplayItem displayItem = item.Value as DisplayItem;
                //    if (displayItem != null) {
                //        string wmMaterialItemDdo = displayItem.DisplayField;
                //        item.CheckState = (wmMaterialItemDdo.IndexOf(tbPaperTemplateInfo.MatName, System.StringComparison.CurrentCultureIgnoreCase) > -1)
                //                            ? CheckState.Checked
                //                            : CheckState.Unchecked;
                //    }
                //}
            }
            Cursor = Cursors.Default;
        }
        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            DrawScaleLine();
            DrawRedLine();
            axPanelPB.Width = Width - axPanelPB.Location.X - 30;

            //			xtraTabControl1.Height = Height - statusStrip1.Height - xtraTabControl1.Location.Y - 36;
            pnlPhotoInfo.Height = Height - statusStrip1.Height - pnlPhotoInfo.Location.Y - 50;
            pnlPhotoInfo.Width = Width - pnlPhotoInfo.Location.X - 30;
        }
        private void xtraTabControl1_SelectedPageChanged(object sender, TabPageChangedEventArgs e)
        { //在商户、尺寸、清柜之前切换
        }
        private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
        { //改变颜色
            if (e.RowHandle >= 0)
            {
                PhotoInfo_PinBan curPi = (PhotoInfo_PinBan)gridView1.GetRow(e.RowHandle);
                if (curPi.IsInBoard)
                {
                    e.Appearance.BackColor = Color.GreenYellow;
                }
                else if (curPi.Priority > 6)
                {
                    e.Appearance.BackColor = Color.Orange;
                }
            }
        }
        private void 清空_Click(object sender, EventArgs e)
        {
            _presenter.ClearBigBoard();
        }
        private void timer1_Tick_1(object sender, EventArgs e) //定时绘制红线
        {
            //this.DrawRedLine();//			PnlBigBoard.Refresh();
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        { //是否显示图片
            Cursor = Cursors.WaitCursor;
            DataTransfer.IsShowImg = checkBox1.Checked;
            _presenter.ShowImgChanged();
            Cursor = Cursors.Default;
        }
        private void LBShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LBShop.Focused)
            {
                _presenter.FilterShop();
            }
        }
        private void LBShop_AfterSelect(object sender, TreeViewEventArgs e)
        { //筛选商户
            if (LBShop.Focused)
            {
                _presenter.FilterShop();
            }
        }
        private void LBSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LBSize.Focused)
            {
                _presenter.FilterPhotoSize();
            }
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            //true是只看清柜的，false看全部的
            if (checkBox3.Checked)
            {
                _presenter.BindClearBoxList();
            }
            else
            {
                _presenter.RushBindPhotos(true, chkShowAllPaperTypePhotos.Checked);
            }
        }
        private void toolStripButton5_Click(object sender, EventArgs e)
        { //打印
            Cursor = Cursors.WaitCursor;
            toolStripButton5.Enabled = false;
            IEnumerable<RectangeView> rectangeInfo = axPanelPB.GetRectangeInfo().Cast<RectangeView>();
            float wastePercent = (float)axPanelPB.GetWastePercent();
            _presenter.SetWraste(wastePercent);
            //			WM_MaterialItemDDO wmMaterialItemDdo = cbPaperMat.GetSelectItem2OfType<WM_MaterialItemDDO>();
            _presenter.PintCurBoard(rectangeInfo, wastePercent);
            //			_presenter.PintCurBoard(rectangeInfo, wastePercent);
            _presenter.RushBindPhotos(chkShowAllPaperTypePhotos.Checked);
            FillOtherPaperPhoto();
            toolStripButton5.Enabled = true;
            Cursor = Cursors.Default;
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        { //新建模板
            try
            {
                _presenter.SelectTemplate();
            }
            catch (Exception ex)
            {
                //ComControls.Extension.MessageBox.Error(ex);
            }
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        { //刷新
            Cursor = Cursors.WaitCursor;
            _presenter.RushBindPhotos(chkShowAllPaperTypePhotos.Checked);
            FillOtherPaperPhoto();
            Cursor = Cursors.Default;
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        { //半自动拼板
            Cursor = Cursors.WaitCursor;
            toolStripButton3.Enabled = false;
            try
            {
                _presenter.semiAutoPB();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                toolStripButton3.Enabled = true;
                Cursor = Cursors.Default;
            }
            float wastePercent = (float)axPanelPB.GetWastePercent();
            _presenter.SetWraste(wastePercent);
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        { //全自动拼板
            _presenter.FAutoPB();
        }
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        { //需要清理DataTransfer中的数据
            _presenter.ClearDataTransferData();
        }
        private void toolStripButton6_Click(object sender, EventArgs e)
        { //打开通迅模块
            _presenter.ShowConnectionFrm();
        }
        private void toolStripButton7_Click(object sender, EventArgs e)
        { //转发图片
            _presenter.SendToOtherPrinter();
        }
        private void checkBox2_CheckedChanged_1(object sender, EventArgs e)
        { //改变条码的方向
            //_presenter.ClearBigBoard();
            if (checkBox2.Checked)
            {
                //条码在下向
                DataTransfer.BarCodeStatus = 0;
            }
            else
            {
                DataTransfer.BarCodeStatus = 1;
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "纵向":
                    DataTransfer.BarCodeStatus = (int)PhotoBarCodeStatus.RightOrLeft;
                    break;
                case "横向":
                    DataTransfer.BarCodeStatus = (int)PhotoBarCodeStatus.TopOrBottom;
                    break;
                case "无备注":
                    DataTransfer.BarCodeStatus = (int)PhotoBarCodeStatus.NoBarCode;
                    break;
                default:
                    DataTransfer.BarCodeStatus = (int)PhotoBarCodeStatus.NoBarCode;
                    break;
            }

            //			if (comboBox1.Text == "左方") {
            //				//条码在下向
            //				DataTransfer.BarCodeStatus = 1;
            //			}
            //			else if (comboBox1.Text == "上方") {
            //				DataTransfer.BarCodeStatus = 0;
            //			}
            //			else if (comboBox1.Text == "无") {
            //				DataTransfer.BarCodeStatus = 2;
            //			}
            //			else {
            //				DataTransfer.BarCodeStatus = 3;
            //			}
        }
        private void cmboxOrderNum_TextChanged(object sender, EventArgs e)
        {
            //改变显示的数量
            int changingNum = 30; //默认是30
            try
            {
                changingNum = Convert.ToInt32(cmboxOrderNum.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入数字");
                cmboxOrderNum.SelectedIndex = 0;
                return;
            }
            if (changingNum <= 0)
            {
                MessageBox.Show("请输出大于0的数字");
                return;
            }
            DataTransfer.ShowOrderNum = changingNum;
            ModifyConfigurationOP.SetConfig("PBorderNum", changingNum.ToString());
        }
        private void cmboxOrderNum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmboxOrderNum.Text == "全部")
            {
                DataTransfer.ShowOrderNum = -1; //代表全部
            }
            else
            {
                try
                {
                    DataTransfer.ShowOrderNum = Convert.ToInt32(cmboxOrderNum.Text);
                }
                catch (Exception)
                {
                    DataTransfer.ShowOrderNum = 100;
                }
            }
        }
        public void AddPicToBand(params PhotoInfo_PinBan[] bands)
        {
            List<PhotoInfo_PinBan> templist = new List<PhotoInfo_PinBan>();
            foreach (PhotoInfo_PinBan selectedPi in bands)
            {
                if (selectedPi.IsInBoard)
                {
                    return;
                }
                templist.Add(selectedPi);
            }

            //_presenter.NewDrawPic(templist, new Point(0, 0));
            RectangeView[] arrayRect = new RectangeView[templist.Count];
            for (int i = 0; i < templist.Count; i++)
            {
                arrayRect[i].BarcodePixelWidth = DataTransfer.BarCodePixWidth;
                arrayRect[i].BarcodePlace = (int)DataTransfer.BarCodeStatus;
                arrayRect[i].DPI = DataTransfer.CurPrinter.PDPI;
                arrayRect[i].filename = templist[i].PicPath;
                if (comboBox3.SelectedIndex == 1) //横向放
                {
                    if (templist[i].PixWidth >= templist[i].PixHeight)
                    {
                        arrayRect[i].isRotate = 0;
                    }
                    else
                    {
                        arrayRect[i].isRotate = 1;
                    }
                }
                else if (comboBox3.SelectedIndex == 2) //纵向放
                {
                    if (templist[i].PixWidth >= templist[i].PixHeight)
                    {
                        arrayRect[i].isRotate = 1;
                    }
                    else
                    {
                        arrayRect[i].isRotate = 0;
                    }
                }
                else if (comboBox3.SelectedIndex == 0)
                {
                    arrayRect[i].isRotate = 0;
                }
                else
                {
                    arrayRect[i].isRotate = -1;
                }
                arrayRect[i].PictureHeight = Convert.ToInt32((decimal)templist[i].Height * DataTransfer.CurPrinter.PDPI);
                arrayRect[i].PictureID = templist[i].PhotoId;
                arrayRect[i].PictureWidth = Convert.ToInt32((decimal)templist[i].Width * DataTransfer.CurPrinter.PDPI);
                //arrayRect[i].Unit = ComboBoxUnit.SelectedIndex;
            }
            Array array = (Array)arrayRect;
            axPanelPB.AddRectange(ref array);
            axPanelPB.Focus();
        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        { //在照片列表中双击一张图片就自动将这张照片放到大板中
            //			//双击一照片就自动将图片放到大板上
            //			var selectedPi = gridView1.GetFocusedRow() as PhotoInfo_PinBan;
            //			if (selectedPi != null) {
            //				if (selectedPi.IsInBoard) {
            //					return;
            //				}
            //				var templist = new List<PhotoInfo_PinBan>();
            //				templist.Add(selectedPi);
            //				_presenter.NewDrawPic(templist, new Point(0, 0));
            //			}
            //双击一照片就自动将图片放到大板上

            PhotoInfo_PinBan selectedPi = gridView1.GetFocusedRow() as PhotoInfo_PinBan;
            if (selectedPi != null)
            {
                AddPicToBand(selectedPi);
            }
        }
        private void LBSize_DoubleClick(object sender, EventArgs e)
        { //双击某一尺寸信息，就自动将这种尺寸的全部照片放到大板中, 一次三张
            _presenter.AutoSelectedCurGridThreePicToBoard();
        }
        private void timerReushBoardStatus_Tick(object sender, EventArgs e)
        {
            RushBoardStatus();
        }
        private void RushBoardStatus()
        {
            pnlBoardStatus.Controls.Clear();

            //从数据库中获取目前的大板状态的数量
            int lblIndex = 0, lblWidth = 18;
            Point lblLocation = new Point(5, 5);
            List<string> listMsg = BoardStatsMonitorBLL.GetAllBoardStausAndQutiy();
            foreach (string msg in listMsg)
            {
                Label lbl = new Label();
                lbl.AutoSize = true;
                lbl.Name = "lbl" + lblIndex;
                lbl.Text = msg;
                if (lblIndex != 0)
                {
                    lblLocation = new Point(5, lblLocation.Y + lblWidth + 5);
                }
                lblIndex++;
                lbl.Location = lblLocation;
                if (msg.Contains("错误"))
                {
                    lbl.ForeColor = Color.Red;
                }
                pnlBoardStatus.Controls.Add(lbl);
            }
        }
        private void comboBoxSizeUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawScaleLine();
            Settings.Default.SizeUint = comboBoxSizeUnit.SelectedIndex;
            Settings.Default.Save();
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //			if (comboBox2.SelectedIndex == 0) {
            //				perPixel = DataTransfer.perPix = 10;
            //			}
            //			else {
            //				perPixel = DataTransfer.perPix = 5;
            //			}
            //			DrawScaleLine();
            //			cmbTemplate_DropDownClosed(sender, e);

        }
        private void chkShowAllPaperTypePhotos_CheckedChanged(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            _showAllPapertypePhotos = chkShowAllPaperTypePhotos.Checked;
            try
            {
                _presenter.RushBindPhotos(chkShowAllPaperTypePhotos.Checked);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTransfer.PhotoDirection = comboBox3.SelectedIndex;
        }
        private void cmbTemplate_Click(object sender, EventArgs e)
        {
        }
        private void axPanelPB_RectangeAdd(object sender, __Panel_RectangeAddEvent e)
        {
            int[] picidArray = (int[])e.pictureID;
            //if (picidArray.Length > 0)
            //{
            //    List<PhotoInfo_PBDCT> listView = (List<PhotoInfo_PBDCT>)GcPhotoInfo.DataSource;
            //    for (int i = 0; i < picidArray.Length; i++)
            //    {
            //        listView.FirstOrDefault(p => p.PhotoId == picidArray[i]).IsInBoard = true;
            //    }
            //}
            if (!_displayAuto)
            {
                _presenter.ChangePhotoStatus(picidArray, true);
            }
            else
            {
                _displayAuto = false;
            }
            gridView1.RefreshData();
            // toolStripLabel10.Text = (axPanelPB.GetWastePercent()*100).ToString("0.00");
        }
        private void axPanelPB_RectangeClear(object sender, __Panel_RectangeClearEvent e)
        {
            int[] picidArray = (int[])e.pictureID;
            //if (picidArray.Length > 0)
            //{
            //    List<PhotoInfo_PBDCT> listView = (List<PhotoInfo_PBDCT>)GcPhotoInfo.DataSource;
            //    for (int i = 0; i < picidArray.Length; i++)
            //    {
            //        listView.FirstOrDefault(p => p.PhotoId == picidArray[i]).IsInBoard = false;
            //    }

            //}
            //GcPhotoInfo.RefreshDataSource();
            if (!_displayAuto)
            {
                _presenter.ChangePhotoStatus(picidArray, false);
            }
            else
            {
                _displayAuto = false;
            }
            gridView1.RefreshData();
        }
        private void axPanelPB_RectangeDelete(object sender, __Panel_RectangeDeleteEvent e)
        {
            int[] picidArray = (int[])e.pictureID;
            if (!_displayAuto)
            {
                _presenter.ChangePhotoStatus(picidArray, false);
            }
            else
            {
                _displayAuto = false;
            }
            gridView1.RefreshData();
        }
        private void axPanelPB_WastePercentChange(object sender, __Panel_WastePercentChangeEvent e)
        {
            //			lblWate.Text = string.Format("{0:f2}%", (decimal) e.wastePercent*100);
            _presenter.SetWraste((float)e.wastePercent);
        }

        #region 多选图片

        private void PnlBigBoard_MouseDown_1(object sender, MouseEventArgs e)
        {
            if (HaveAPanel)
            {
                //撤消多选
                _presenter.CancelMutilSelect();
                HaveAPanel = false;
            }
            MouseIsDown = true;
            DrawStart(e.Location);
        }
        private void PnlBigBoard_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (MouseIsDown)
            {
                //				e.Location.Offset(PnlBigBoard.Location);
                ResizeToRectangle(e.Location);
            }
        }
        private void PnlBigBoard_MouseUp_1(object sender, MouseEventArgs e)
        {
            //			int minX, minY, maxX, maxY;
            //			//			PnlBigBoard.Capture = false;
            //			Cursor.Clip = Rectangle.Empty;
            //			MouseIsDown = false;
            //			DrawRectangle();
            //			MouseRect = rectCorrection(MouseRect);
            //			//			foreach(Control ct in PnlBigBoard.Controls) {
            //			//				//if ((ct is PicView) && (MouseRect.Contains(ct.Location) || MouseRect.Contains(new Point(ct.Location.X + ct.Width, ct.Location.Y + ct.Height))))
            //			//				if ((ct is PicView) && MouseRect.IntersectsWith(((PicView) ct).Rect)) {
            //			//					selectedCtrls.Add((PicView) ct);
            //			//				}
            //			//			}
            //			if (selectedCtrls.Count <= 1) {
            //				selectedCtrls.Clear();
            //				return;
            //			}
            //			minX = selectedCtrls.Min(c => c.Location.X);
            //			minY = selectedCtrls.Min(c => c.Location.Y);
            //			maxX = selectedCtrls.Max(c => c.Location.X + c.Width);
            //			maxY = selectedCtrls.Max(c => c.Location.Y + c.Height);
            //			PicView pv = new PicView(maxX - minX, maxY - minY, new Point(minX, minY));
            //			_presenter.MultiSelectedPic(pv, selectedCtrls);
            //			ShowPic(pv);
            //			HaveAPanel = true;
            //			MouseRect = Rectangle.Empty;
        }

        #region 框选的 help methods

        //修正MouseRect 的值
        private Rectangle rectCorrection(Rectangle oldRect)
        {
            if (oldRect.Width < 0)
            {
                oldRect.X += oldRect.Width;
                oldRect.Width *= -1;
            }
            if (oldRect.Height < 0)
            {
                oldRect.Y += oldRect.Height;
                oldRect.Height *= -1;
            }
            return new Rectangle(oldRect.X, oldRect.Y, oldRect.Width, oldRect.Height);
        }
        private void ResizeToRectangle(Point p)
        {
            DrawRectangle();
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle();
        }
        private void DrawRectangle()
        {
            //			Rectangle rect = PnlBigBoard.RectangleToScreen(MouseRect);
            //			ControlPaint.DrawReversibleFrame(rect, Color.YellowGreen, FrameStyle.Dashed);
        }
        private void DrawStart(Point StartPoint)
        {
            //			PnlBigBoard.Capture = true;
            //			//Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
            //			Cursor.Clip = PnlBigBoard.RectangleToScreen(new Rectangle(0, 0, PnlBigBoard.Width, PnlBigBoard.Height));
            //			MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }

        #endregion

        #endregion

        #region IMainView 成员

        private readonly Panel pRedLine = new Panel();
        private string _selectCtmName;
        private string _selectOrderProductName;
        private string _selectShopName;
        private bool _showAllPapertypePhotos;
        public string SelectShop
        {
            get
            {
                _selectShopName = "";
                _selectCtmName = "";
                _selectOrderProductName = "";
                TreeNode selNode = LBShop.SelectedNode;
                if (selNode == null)
                {
                    return _selectShopName;
                }
                int level = (int)selNode.Tag;
                if (level == 1)
                { //选择的是商户
                    _selectShopName = selNode.Text.Trim();
                    return _selectShopName;
                }
                if (level == 2)
                { //选择的是客户
                    _selectCtmName = selNode.Text.Trim();
                    _selectShopName = selNode.Parent.Text.Trim();
                    return _selectShopName;
                }
                if (level == 3)
                { //选择的是产品名称
                    _selectCtmName = selNode.Parent.Text.Trim();
                    _selectOrderProductName = selNode.Text.Trim();
                    _selectShopName = selNode.Parent.Parent.Text.Trim();
                    return _selectShopName;
                }
                return null;
            }
        }
        public string SelectPhotoSize
        {
            get { return LBSize.SelectedItem.ToString(); }
        }
        public void ShowWaste(string strWaste, Color c)
        { //显示浪费率
            lblWate.Text = strWaste;
            lblWate.ForeColor = c;
        }
        public void DrawScaleLine()
        { //画刻度线
            //PnlBigBoard.Height = Convert.ToInt32(perPixel * DataTransfer.curTemplate);
            if (comboBoxSizeUnit.SelectedIndex == 1)
            {
                int startX = axPanelPB.Left;
                int startY = axPanelPB.Top;
                int currentX = startX;
                int currentY = startY;
                int num = 0;
                Graphics g = CreateGraphics();
                g.Clear(BackColor);
                Pen p = new Pen(Color.Black, 1);
                while (num * perPixel < axPanelPB.Width)
                {
                    switch (num % 4)
                    {
                        case 0:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 20);
                            Rectangle rect = new Rectangle(currentX + 1, currentY - 20, 25, 15);
                            Font aFont = new Font("Arial", 6, FontStyle.Regular);
                            g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                            aFont.Dispose();
                            break;
                        case 1:
                        case 3:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 8);
                            break;
                        case 2:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 12);
                            break;
                        default:
                            break;
                    }
                    num++;
                    currentX += Convert.ToInt32(perPixel);
                }
                currentX = startX;
                currentY = startY;
                num = 0;
                while (num * perPixel <= axPanelPB.Height)
                {
                    if (num * perPixel == axPanelPB.Height)
                    {
                        g.DrawLine(p, currentX, currentY, currentX - 20, currentY);
                        Rectangle rect = new Rectangle(currentX - 25 + 1, currentY + 1, 15, 15);
                        Font aFont = new Font("Arial", 6, FontStyle.Regular);
                        g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                        aFont.Dispose();
                    }
                    else
                    {
                        switch (num % 4)
                        {
                            case 0:
                                g.DrawLine(p, currentX, currentY, currentX - 20, currentY);
                                Rectangle rect = new Rectangle(currentX - 25 + 1, currentY + 1, 15, 15);
                                Font aFont = new Font("Arial", 6, FontStyle.Regular);
                                g.DrawString(num.ToString(), aFont, Brushes.Black, rect);
                                aFont.Dispose();
                                break;
                            case 1:
                            case 3:
                                g.DrawLine(p, currentX, currentY, currentX - 8, currentY);
                                break;
                            case 2:
                                g.DrawLine(p, currentX, currentY, currentX - 12, currentY);
                                break;
                            default:
                                break;
                        }
                    }
                    num++;
                    currentY += Convert.ToInt32(perPixel);
                }
                p.Dispose();
            }
            else
            {
                int startX = axPanelPB.Left;
                int startY = axPanelPB.Top;
                int currentX = startX;
                int currentY = startY;
                int num = 0;
                Graphics g = CreateGraphics();
                g.Clear(BackColor);
                Pen p = new Pen(Color.Black, 1);
                while (num * perPixel < axPanelPB.Width)
                {
                    switch (num % 4)
                    {
                        case 0:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 20);
                            Rectangle rect = new Rectangle(currentX + 1, currentY - 20, 30, 15);
                            Font aFont = new Font("Arial", 8, FontStyle.Regular);
                            g.DrawString((num * 2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                            aFont.Dispose();
                            break;
                        case 1:
                        case 3:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 8);
                            break;
                        case 2:
                            g.DrawLine(p, currentX, currentY, currentX, currentY - 12);
                            break;
                        default:
                            break;
                    }
                    num++;
                    currentX += Convert.ToInt32(perPixel);
                }
                currentX = startX;
                currentY = startY;
                num = 0;
                while (num * perPixel <= axPanelPB.Height)
                {
                    if (num * perPixel == axPanelPB.Height)
                    {
                        g.DrawLine(p, currentX, currentY, currentX - 20, currentY);
                        Rectangle rect = new Rectangle(currentX - 25 + 1, currentY + 1, 30, 15);
                        Font aFont = new Font("Arial", 7, FontStyle.Regular);
                        g.DrawString((num * 2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                        aFont.Dispose();
                    }
                    else
                    {
                        switch (num % 4)
                        {
                            case 0:
                                g.DrawLine(p, currentX, currentY, currentX - 20, currentY);
                                Rectangle rect = new Rectangle(currentX - 25 + 1, currentY + 1, 30, 15);
                                Font aFont = new Font("Arial", 7, FontStyle.Regular);
                                g.DrawString((num * 2.54).ToString("0.0"), aFont, Brushes.Black, rect);
                                aFont.Dispose();
                                break;
                            case 1:
                            case 3:
                                g.DrawLine(p, currentX, currentY, currentX - 8, currentY);
                                break;
                            case 2:
                                g.DrawLine(p, currentX, currentY, currentX - 12, currentY);
                                break;
                            default:
                                break;
                        }
                    }
                    num++;
                    currentY += Convert.ToInt32(perPixel);
                }
                p.Dispose();
            }
        }
        public void InitCtlsLoaction()
        { //调整相关控件位置axPanelPB.Left = xtraTabControl1.Right + 25;
            axPanelPB.Top = toolStrip1.Height + 25; //new 25 表示用来画刻度的宽度 
            axPanelPB.Height = Convert.ToInt32(DataTransfer.curTemplate.BoardHeight * perPixel);
            pnlPhotoInfo.Top = axPanelPB.Bottom + 7; //new
            pnlPhotoInfo.Height = Height - pnlPhotoInfo.Top - statusStrip1.Height - 36;
            cmbTemplate.Text = DataTransfer.curTemplate.TemplateName;
        }
        public void BindCmbTempate(List<Tb_paperTemplate_Info> paperTemplateList)
        { //绑定模板下拉列表
            cmbTemplate.Items.Clear();
            foreach (Tb_paperTemplate_Info pt in paperTemplateList)
            {
                cmbTemplate.Items.Add(pt.TemplateName);
            }
        }
        public void BindMainGrid(List<PhotoInfo_PinBan> _photosList)
        { //绑定“下面”主要的照片列表
            GcPhotoInfo.DataSource = _photosList;
            lblPhotoCount.Text = "   照片总数：" + _photosList.Count;
        }
        public void BindShopList(List<string> shopNameList)
        { //绑定商户列表
            //xtraTabControl1.SelectedTabPageIndex = 0;
            ////LBShop.Items.Clear();
            ////foreach (var shopName in shopNameList)
            ////{
            ////    LBShop.Items.Add(shopName);
            ////}
        }
        public void BindPhotoSizeList(List<string> photoSizeList)
        { //绑定照片尺寸列表
            LBSize.Items.Clear();
            LBSize.DataSource = photoSizeList;
            LBSize.DisplayMember = "";
        }
        public void BindClearGrid(List<PhotoInfo_PinBan> clearBoxPhotos)
        { //绑定清柜的照片列表
            GcPhotoInfo.DataSource = clearBoxPhotos;
        }
        public string SelectTemplateName
        { //选择的模板名称
            get
            {
                if (cmbTemplate.SelectedItem == null)
                {
                    return null;
                }
                return cmbTemplate.SelectedItem.ToString().Trim();
            }
        }
        public bool ShowAllPapertypePhotos
        {
            get { return _showAllPapertypePhotos; }
            set { _showAllPapertypePhotos = chkShowAllPaperTypePhotos.Checked; }
        }
        public void ShowStatusMsg(string msg)
        { //显示状态信息
            lblMes.Text = msg;
        }
        public void ShowPic(Array array)
        { //将图片显示在大纸上
            bool isDisplay = false;
            _displayAuto = true;
            axPanelPB.ClearRectange();
            axPanelPB.AddRectange(ref array, ref isDisplay);
        }
        public void BindShopList(List<BindShopInfo> listBindShop)
        {
            xtraTabControl1.SelectedTabPageIndex = 0;
            LBShop.Nodes.Clear();
            if (listBindShop.Count > 0)
            {
                LBShop.Nodes.Add(new TreeNode { Text = "全部", Tag = 1 });
            }
            TreeNode selNode = null;
            if (!checkBoxPinFlag.Checked)
            {
                bool isPriority = false;
                foreach (BindShopInfo shop in listBindShop)
                {
                    isPriority = false;
                    TreeNode shopNode = LBShop.Nodes.Add(shop.ShopName);
                    shopNode.Tag = 1;
                    if (!string.IsNullOrEmpty(_selectShopName))
                    {
                        if (shopNode.Text == _selectShopName)
                        {
                            selNode = shopNode;
                        }
                    }
                    foreach (BindCtmInfo ctm in shop.Ctms)
                    {

                        bool ctmIsPriority = false;
                        TreeNode ctmNode = shopNode.Nodes.Add(ctm.CtmName);
                        ctmNode.Tag = 2;
                        if (!string.IsNullOrEmpty(_selectCtmName))
                        {
                            if (ctmNode.Text == _selectCtmName)
                            {
                                selNode = ctmNode;
                            }
                        }
                        foreach (BindOrderInfo orderProductBind in ctm.OrderList)
                        {
                            TreeNode orderNode = ctmNode.Nodes.Add(orderProductBind.OrderProductName);
                            if (orderProductBind.IsPriority)
                            {
                                ctmIsPriority = true;
                                isPriority = true;
                                orderNode.BackColor = Color.Red;
                                orderNode.ForeColor = Color.YellowGreen;
                            }
                            if (orderProductBind.IsPart)
                            {
                                orderNode.BackColor = Color.MediumVioletRed;
                                orderNode.ForeColor = Color.YellowGreen;
                            }
                            orderNode.Tag = 3;
                            if (!string.IsNullOrEmpty(_selectOrderProductName))
                            {
                                if (orderProductBind.OrderProductName == _selectOrderProductName)
                                {
                                    selNode = orderNode;
                                }
                            }
                        }
                        if (ctmIsPriority)
                        {
                            ctmNode.BackColor = Color.Red;
                            ctmNode.ForeColor = Color.YellowGreen;
                        }
                    }
                    if (isPriority)
                    {
                        shopNode.BackColor = Color.Red;
                        shopNode.ForeColor = Color.YellowGreen;
                    }
                }
                if (selNode != null)
                {
                    selNode.Expand();
                }
            }
            else
            {
                bool isPart = false;
                foreach (BindShopInfo shop in listBindShop)
                {
                    isPart = false;
                    TreeNode shopNode = LBShop.Nodes.Add(shop.ShopName);
                    shopNode.Tag = 1;
                    if (!string.IsNullOrEmpty(_selectShopName))
                    {
                        if (shopNode.Text == _selectShopName)
                        {
                            selNode = shopNode;
                        }
                    }
                    foreach (BindCtmInfo ctm in shop.Ctms)
                    {
                        bool ctmIsPart = false;
                        isPart = false;
                        TreeNode ctmNode = shopNode.Nodes.Add(ctm.CtmName);
                        ctmNode.Tag = 2;
                        if (!string.IsNullOrEmpty(_selectCtmName))
                        {
                            if (ctmNode.Text == _selectCtmName)
                            {
                                selNode = ctmNode;
                            }
                        }
                        foreach (BindOrderInfo orderProductBind in ctm.OrderList)
                        {
                              isPart = false;
                            TreeNode orderNode = ctmNode.Nodes.Add(orderProductBind.OrderProductName);
                            if (orderProductBind.IsPart)
                            {
                                ctmIsPart = true;
                                isPart = true;
                                orderNode.BackColor = Color.Tomato;
                                orderNode.ForeColor = Color.YellowGreen;
                            }
                            orderNode.Tag = 3;
                            if (!string.IsNullOrEmpty(_selectOrderProductName))
                            {
                                if (orderProductBind.OrderProductName == _selectOrderProductName)
                                {
                                    selNode = orderNode;
                                }
                            }
                        }
                        if (ctmIsPart)
                        {
                            ctmNode.BackColor = Color.Tomato;
                            ctmNode.ForeColor = Color.YellowGreen;
                        }
                    }
                    if (isPart)
                    {
                        shopNode.BackColor = Color.Tomato;
                        shopNode.ForeColor = Color.YellowGreen;
                    }
                }
                if (selNode != null)
                {
                    selNode.Expand();
                }
            }
            LBShop.SelectedNode = selNode;
            _presenter.FilterShop();
        }
        public string SelectCtmName
        {
            get { return _selectCtmName; }
            set { _selectCtmName = value; }
        }
        public string SelectOrderProductName
        {
            get { return _selectOrderProductName; }
            set { _selectOrderProductName = value; }
        }
        public List<int> SelectPhotoIdList
        {
            get
            {
                List<int> photoIdList = new List<int>();
                foreach (int rowIndex in gridView1.GetSelectedRows())
                {
                    photoIdList.Add(((PhotoInfo_PinBan)gridView1.GetRow(rowIndex)).PhotoId);
                }
                return photoIdList;
            }
        }
        public void DrawRedLine() //画出红线的位置
        {
            //Graphics g = this.PnlBigBoard.CreateGraphics();
            //g.Clear(PnlBigBoard.BackColor);
            //Pen p = new Pen(Color.Red, 1);
            if (DataTransfer.curTemplate != null)
            {
                //int x = Convert.ToInt32(DataTransfer.curTemplateInfo.Length * DataTransfer.PERPIXEL);
                //int y = Convert.ToInt32(DataTransfer.curTemplateInfo.Height * DataTransfer.PERPIXEL);
                ////g.DrawLine(p, x, 0, x, y);

                //pRedLine.Height = y;
                //pRedLine.Location = new Point(x + 1, 0);
                //this.PnlBigBoard.Controls.Add(pRedLine);
                axPanelPB.ScaleHeight = (float)(DataTransfer.curTemplate.BoardHeight * DataTransfer.CurPrinter.PDPI);
                if (axPanelPB.Width != 0)
                {
                    axPanelPB.ScaleWidth = axPanelPB.ScaleHeight * axPanelPB.Width / axPanelPB.Height;
                }
                axPanelPB.MaxWidth = Convert.ToInt32(DataTransfer.curTemplate.BoardLength * DataTransfer.CurPrinter.PDPI);
            }
            //			//Graphics g = this.PnlBigBoard.CreateGraphics();
            //			//g.Clear(PnlBigBoard.BackColor);
            //			//Pen p = new Pen(Color.Red, 1);
            //			//int x = Convert.ToInt32(DataTransfer.curTemplate.BoardLength * perPixel);
            //			//int y = Convert.ToInt32(DataTransfer.curTemplate.BoardHeight * perPixel);
            //			//g.DrawLine(p, x, 0, x, y);
            //			if (DataTransfer.curTemplate != null) {
            //				int x = DataTransfer.inchBoardWidth;
            //				int y = DataTransfer.inchBoardHeight;
            //				pRedLine.Height = y;
            //				pRedLine.Location = new Point(x + 1, 0);
            //				PnlBigBoard.Controls.Add(pRedLine);
            //			}
        }

        #endregion

        #region 拖拽主列表中的照片的操r

        private GridHitInfo downHitInfo;
        public void ClearBigBoard()
        {
            axPanelPB.ClearRectange();
            //			PnlBigBoard.Controls.Clear();
            gridView1.RefreshData();
            ShowWaste("100%", Color.Red);
        }
        public void RefushMainGrid()
        {
            gridView1.RefreshData();
        }
        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            //GridView view = sender as GridView;
            //downHitInfo = null;
            //GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            //if (ModifierKeys != Keys.None)
            //{
            //    return;
            //}
            //if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator)
            //{
            //    if (view.GetSelectedRows().Any(c => c == hitInfo.RowHandle))
            //    {
            //        List<PhotoInfo_PinBan> photoInfoPinBans = view.GetSelectedRows<PhotoInfo_PinBan>();
            //        if (photoInfoPinBans.Count > 1)
            //        {
            //            foreach (PhotoInfo_PinBan selectedPi in photoInfoPinBans)
            //            {
            //                if (selectedPi != null)
            //                {
            //                    AddPicToBand(selectedPi);
            //                }
            //            }
            //            view.ClearSelection();
            //        }
            //    }
            //    downHitInfo = hitInfo;
            //}
            GridView view = sender as GridView;
            downHitInfo = null;

            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));

            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.HitTest != GridHitTest.RowIndicator)
                downHitInfo = hitInfo;

        }
        private string GetSelectedPhotoString(GridView view)
        {
            string inputStr = "";
            List<PhotoInfo_PinBan> SelectedPhotos = GetDragData(view).OrderByDescending(or => or.Height).ToList();
            RectangeView[] arrayRect = new RectangeView[SelectedPhotos.Count];
            for (int i = 0; i < SelectedPhotos.Count; i++)
            {
                arrayRect[i].BarcodePixelWidth = DataTransfer.BarCodePixWidth;
                arrayRect[i].BarcodePlace = DataTransfer.BarCodeStatus;
                arrayRect[i].DPI = DataTransfer.CurPrinter.PDPI;
                arrayRect[i].filename = SelectedPhotos[i].PicPath;
                if (comboBox3.SelectedIndex == 1) //横向放
                {
                    if (SelectedPhotos[i].PixWidth >= SelectedPhotos[i].PixHeight)
                    {
                        arrayRect[i].isRotate = 0;
                    }
                    else
                    {
                        arrayRect[i].isRotate = 1;
                    }
                }
                else if (comboBox3.SelectedIndex == 2) //纵向放
                {
                    if (SelectedPhotos[i].PixWidth >= SelectedPhotos[i].PixHeight)
                    {
                        arrayRect[i].isRotate = 1;
                    }
                    else
                    {
                        arrayRect[i].isRotate = 0;
                    }
                }
                else if (comboBox3.SelectedIndex == 0)
                {
                    arrayRect[i].isRotate = 0;
                }
                else
                {
                    arrayRect[i].isRotate = -1;
                }
                arrayRect[i].PictureHeight = Convert.ToInt32((decimal)SelectedPhotos[i].Height * DataTransfer.CurPrinter.PDPI);
                arrayRect[i].PictureID = SelectedPhotos[i].PhotoId;
                arrayRect[i].PictureWidth = Convert.ToInt32((decimal)SelectedPhotos[i].Width * DataTransfer.CurPrinter.PDPI);
                inputStr += axPanelPB.RectangeToString(ref arrayRect[i]);
            }
            return inputStr;
        }
        private void gridView1_MouseMove(object sender, MouseEventArgs e)
        {
            //			return;


            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
                string inputStr = "";
                if (!dragRect.Contains(new Point(e.X, e.Y)))//当他移出这个区域后就开始拖操作
                {
                    List<PhotoInfo_PinBan> templist = GetDragData(view).OrderByDescending(or => or.Height).ToList();
                    RealLayoutPanel.RectangeView[] arrayRect = new RealLayoutPanel.RectangeView[templist.Count];
                    for (int i = 0; i < templist.Count; i++)
                    {
                        arrayRect[i].BarcodePixelWidth = DataTransfer.BarCodePixWidth;
                        arrayRect[i].BarcodePlace = (int)DataTransfer.BarCodeStatus;
                        arrayRect[i].DPI = DataTransfer.CurPrinter.PDPI;
                        arrayRect[i].filename = templist[i].PicPath;
                        if (comboBox3.SelectedIndex == 1) //横向放
                        {
                            if (templist[i].PixWidth >= templist[i].PixHeight)
                            {
                                arrayRect[i].isRotate = 0;
                            }
                            else
                            {
                                arrayRect[i].isRotate = 1;
                            }
                        }
                        else if (comboBox3.SelectedIndex == 2) //纵向放
                        {
                            if (templist[i].PixWidth >= templist[i].PixHeight)
                            {
                                arrayRect[i].isRotate = 1;
                            }
                            else
                            {
                                arrayRect[i].isRotate = 0;
                            }
                        }
                        else if (comboBox3.SelectedIndex == 0)
                        {
                            arrayRect[i].isRotate = 0;
                        }
                        else
                        {
                            arrayRect[i].isRotate = -1;
                        }
                        arrayRect[i].PictureHeight = Convert.ToInt32((decimal)templist[i].Height * DataTransfer.CurPrinter.PDPI);
                        arrayRect[i].PictureID = templist[i].PhotoId;
                        arrayRect[i].PictureWidth = Convert.ToInt32((decimal)templist[i].Width * DataTransfer.CurPrinter.PDPI);
                        inputStr += axPanelPB.RectangeToString(ref arrayRect[i]);
                    }

                    view.GridControl.DoDragDrop(inputStr, DragDropEffects.Copy);
                    downHitInfo = null;
                }
            }
        }
        private void gridView1_MouseUp(object sender, MouseEventArgs e)
        {
            downHitInfo = null;
        }
        private List<PhotoInfo_PinBan> GetDragData(GridView view)
        {
            List<PhotoInfo_PinBan> photoIdList = new List<PhotoInfo_PinBan>();
            foreach (int piId in view.GetSelectedRows())
            {
                PhotoInfo_PinBan pi = (PhotoInfo_PinBan)view.GetRow(piId);
                if (pi.IsInBoard == false)
                {
                    photoIdList.Add(pi);
                }
            }
            return photoIdList;
        }

        #endregion

        #region 大纸上接收拖上来的图片

        private void PnlBigBoard_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<PhotoInfo_PinBan>)))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        private void PnlBigBoard_DragDrop(object sender, DragEventArgs e)
        {
            //			List<PhotoInfo_PinBan> photoList = (List<PhotoInfo_PinBan>) e.Data.GetData(typeof (List<PhotoInfo_PinBan>)); //接收到的照片实体
            //			//根据这些照片ID把他们放在应放的位置上
            //			Point currentPicLoacation = PnlBigBoard.PointToClient(new Point(e.X, e.Y)); //currentPicLocation的从标为图片相对于大板的坐标
            //			//_presenter.DrawPic(photoList, currentPicLoacation);
            //			_presenter.NewDrawPic(photoList, currentPicLoacation);
        }

        #endregion

        private void cbBarCodeWidth_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTransfer.BarCodePixWidth = Convert.ToInt32((Convert.ToDouble(cbBarCodeWidth.Text) / 100 / 2.54) * DataTransfer.CurPrinter.PDPI);
            IniFile.Ini.IniWriteValue("PB", "BarCodeWidth", cbBarCodeWidth.SelectedIndex.ToString());
        }
        //private void cbPaperMat_EditValueChanged(object sender, EventArgs e) {
        //    FillOtherPaperPhoto();
        //}
        private void FillOtherPaperPhoto()
        {
            //			_presenter.SelectedTemplate();
            //List<int> checkedObjectItemOfType = cbPaperMat.GetCheckedObjectItemOfType<int>();
            ////			var photoInfoDdos = photoInfoDDO.GetWaitPBPhoto(DataTransfer.curTemplate.printerId, checkedObjectItemOfType.ToArray());
            //if (_presenter != null) {
            //    _presenter.FillOtherMatPhoto(checkedObjectItemOfType.ToArray());
            //}
        }
        private void chkShowPic_CheckedChanged(object sender, EventArgs e)
        {
            axPanelPB.ShowPicture = chkShowPic.Checked;
            Thread.Sleep(0);
            axPanelPB.Refresh();
        }
        List<PhotoInfo_PinBan> listToSave = new List<PhotoInfo_PinBan>();
        private void ToolStripMenuItemAutoDXF_Click(object sender, EventArgs e)
        {
            listToSave = new List<PhotoInfo_PinBan>();
            string str = Properties.Settings.Default.DxfFolderPath;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = str;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.DxfFolderPath = str = dialog.SelectedPath;
                Properties.Settings.Default.Save();

            }
            List<PhotoInfo_PinBan> listPhoto = GetSelectedPhotoList();
            List<string> dxfFilelist = DXFHelper.GetDxfFile(str);
            foreach (PhotoInfo_PinBan photo in listPhoto)
            {

                string path = DXFHelper.MathFile(dxfFilelist, photo.PicPath);
                if (!string.IsNullOrEmpty(path))
                {
                    photo.DXFPath = path;
                    photo.DXFContent = DXFHelper.GetDXFContent(photo.DXFPath);
                    photo.DXFUnSaved = true;
                }
                listToSave.Add(photo);
            }
            GcPhotoInfo.FocusedView.RefreshData();

        }
        List<PhotoInfo_PinBan> GetSelectedPhotoList()
        {
            List<PhotoInfo_PinBan> listPhoto = new List<PhotoInfo_PinBan>();
            var rowidArr = gridView1.GetSelectedRows();
            foreach (int row in rowidArr)
            {
                PhotoInfo_PinBan pi = gridView1.GetRow(row) as PhotoInfo_PinBan;
                listPhoto.Add(pi);
            }
            return listPhoto;
        }
        private void ToolStripMenuItemSelectDXF_Click(object sender, EventArgs e)
        {

            PhotoInfo_PinBan photoInfoPbdct = gridView1.GetRow(gridView1.FocusedRowHandle) as PhotoInfo_PinBan;
            //WriteLogForCheck.WriteLogFile(Environment.CurrentDirectory.ToString());
            FrmSelectDXF frmAssignDxf = new FrmSelectDXF(photoInfoPbdct);
            if (DialogResult.OK == frmAssignDxf.ShowDialog())
            {
                var list = new List<PhotoInfo_PinBan>();
                list.Add(photoInfoPbdct);
                var r = _presenter.AdddXFfile(list);
                if (r.Count > 0)
                {
                    photoInfoPbdct.DXFPath = "";
                    photoInfoPbdct.DXFContent = null;
                }
                else
                {
                  
                }
                GcPhotoInfo.FocusedView.RefreshData();
            }

        }

        private void contextMenuStripPhoto_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var List = GetSelectedPhotoList();
            if (List.Count == 1)
            {
                ToolStripMenuItemAutoDXF.Enabled = false;
                ToolStripMenuItemSelectDXF.Enabled = true;
            }
            else if (List.Count > 1)
            {
                ToolStripMenuItemAutoDXF.Enabled = true;
                ToolStripMenuItemSelectDXF.Enabled = false;
            }
        }

        private void ToolStripMenuItemSaveAutoMath_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存裁切文件", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                int[] rowids = gridView1.GetSelectedRows();
                var list = new List<PhotoInfo_PinBan>();
                foreach (int irow in rowids)
                {
                    PhotoInfo_PinBan photoInfoPbdct = gridView1.GetRow(irow) as PhotoInfo_PinBan;
                    if (!string.IsNullOrEmpty(photoInfoPbdct.DXFPath))
                    {
                        list.Add(photoInfoPbdct);
                    }
                }
                List<PhotoInfo_PinBan> r = _presenter.AdddXFfile(list);
                list.ForEach(ea => ea.DXFUnSaved = false);
                if (r.Count > 0)
                {
                    foreach (var ph in listToSave)
                    {
                        ph.DXFPath = "";
                        ph.DXFContent = null;
                        
                    }
                }

            }
            GcPhotoInfo.FocusedView.RefreshData();
        }

        private void comboBoxCutType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.BoardType = comboBoxCutType.SelectedIndex;
            Properties.Settings.Default.Save();
        }

        private void ToolStripMenuItemDelete_Click(object sender, EventArgs e)
        {


            int[] rowids = gridView1.GetSelectedRows();
            var list = new List<PhotoInfo_PinBan>();
            foreach (int irow in rowids)
            {
                PhotoInfo_PinBan photoInfoPbdct = gridView1.GetRow(irow) as PhotoInfo_PinBan;
                list.Add(photoInfoPbdct);
            }
            //WriteLogForCheck.WriteLogFile(Environment.CurrentDirectory.ToString());

           
             var r = _presenter.DeletedXFfile(list);
            if (r.Count > 0)
            {

            }
            else
            {
                foreach (var photoInfoPbdct in list)
                {
                    photoInfoPbdct.DXFPath = "";
                    photoInfoPbdct.DXFContent = null;
                    photoInfoPbdct.DXFUnSaved = false;
                }
            }
            GcPhotoInfo.FocusedView.RefreshData();

        }

        private void 自动匹配裁切文件文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listToSave = new List<PhotoInfo_PinBan>();
            string str = Properties.Settings.Default.DxfFolderPath;
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = str;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.DxfFolderPath = str = dialog.SelectedPath;
                Properties.Settings.Default.Save();

            }
            List<PhotoInfo_PinBan> listPhoto = GetSelectedPhotoList();
            List<string> dxfFilelist = DXFHelper.GetDxfFile(str);
            foreach (PhotoInfo_PinBan photo in listPhoto)
            {

                string path = DXFHelper.MathFile(dxfFilelist, photo.OrderInfoName);
                if (!string.IsNullOrEmpty(path))
                {
                    photo.DXFPath = path;
                    photo.DXFContent = DXFHelper.GetDXFContent(photo.DXFPath);
                    photo.DXFUnSaved = true;
                }
                listToSave.Add(photo);
            }
            GcPhotoInfo.FocusedView.RefreshData();
        }
    }
}
